package com.learn.service.impl;

import com.learn.mapper.DeptMapper;
import com.learn.mapper.EmpMapper;
import com.learn.pojo.Dept;
import com.learn.pojo.DeptLog;
import com.learn.service.DeptLogService;
import com.learn.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;
    @Autowired
    EmpMapper EmpMapper;
    @Autowired
    DeptLogService deptLogService;
    @Override
    public List<Dept> list() {

        return deptMapper.list();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Integer id) {
        try {
            deptMapper.deleteById(id);

            int i = 1 / 0;
            EmpMapper.DeleteByDeptId(id);
        }finally {
            DeptLog dl = new DeptLog(null, LocalDateTime.now(), "删除部门id为"+id);
            deptLogService.Insert(dl);
        }
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @Override
    public Dept getDataById(Integer id) {
        return deptMapper.getDataById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
